﻿using System;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace TwitterBEIS_SL3_SLApp
{
    public class Utility
    {

        /// <summary>
        /// Tansfer the twitter UTC format to the relative time format
        /// </summary>
        /// <param name="oringalTimeLine">the twitter create time string <example>Wed Apr 08 14:32:46 +0000 2009</example></param>
        /// <returns>the relative time string. return null if the parse fails</returns>
        static string TimeTransfer(string oringalTimeLine)
        {
            string relativeTimeLine = "";
            string timeFormat = "ddd MMM dd HH:mm:ss zzz yyyy";

            DateTime dtCreate = new DateTime();
            DateTime dtNow = new DateTime();

            int indexOfTimeZone = oringalTimeLine.IndexOfAny(new char[] { '+', '-' }) + 3;
            oringalTimeLine = oringalTimeLine.Insert(indexOfTimeZone, ":");

            // Try to Parse the twitter UTC string
            if (!DateTime.TryParseExact(oringalTimeLine, timeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtCreate))
            {
                return null;
            }

            dtNow = DateTime.Now;
            TimeSpan interval = dtNow.Subtract(dtCreate);

            if (interval.Days > 0)
            {
                relativeTimeLine = string.Format("about {0} days ago", interval.Days);
            }
            else if (interval.Hours > 0)
            {
                relativeTimeLine = string.Format("about {0} hours ago", interval.Hours);
            }
            else if (interval.Minutes > 0)
            {
                relativeTimeLine = string.Format("{0} mintues ago", interval.Minutes);
            }
            // do twitter show this?
            else if (interval.Seconds > 30)
            {
                relativeTimeLine = string.Format("less than a minute ago");
            }
            else
            {
                relativeTimeLine = string.Format("less than {0} seconds ago", interval.Seconds);
            }

#if DEBUG
            Console.WriteLine(relativeTimeLine);
#endif
            return relativeTimeLine;
        }
    }
}
